package org.finesys.common.mp.core.service.impl;

import java.util.List;

import org.finesys.common.mp.core.service.BaseService;
import org.finesys.common.mp.core.utils.QueryWrapperUtils;
import org.springframework.beans.factory.annotation.Value;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

public class BaseServiceImpl<M extends BaseMapper<T>, T> extends ServiceImpl<M, T> implements BaseService<T> {

    @Value("${mybatis-plus.configuration.map-underscore-to-camel-case:true}")
    private boolean underscoreToCamel;


    /**
     * 查询全部
     */
    @Override
    public List<T> findAll() {
        return baseMapper.selectList(Wrappers.emptyWrapper());
    }

    @Override
    public List<T> findList(T t) {
        QueryWrapperUtils.setUnderscoreToCamel(underscoreToCamel);
        QueryWrapper<T> queryWrapper = QueryWrapperUtils.queryWrapper(t);
        return baseMapper.selectList(queryWrapper);
    }

    /**
     * 分页查询
     */
    @Override
    public Page<T> findPage(Page<T> page, T t) {
        QueryWrapperUtils.setUnderscoreToCamel(underscoreToCamel);
        QueryWrapper<T> queryWrapper = QueryWrapperUtils.queryWrapper(t);
        return baseMapper.selectPage(page, queryWrapper);
    }

    @Override
    public T getOne(T t) {
        return super.getOne(Wrappers.query(t), false);
    }
}
